<!--
 * @Descripttion: 
 * @Version: 1.0
 * @Author: 何志龙
 * @Date: 2023-11-29 10:19:36
 * @LastEditors: 何志龙
 * @LastEditTime: 2023-12-04 09:03:47
-->
<!-- 浙里办个人申请v2 -->
<template>
    <div class="ceshi">
        <div class="CUNYUSHEN" v-if="!isshowChengnuoshu">
            <inputSelect :isShowCity="isShowCity" :contentData.sync="contentData" :contentDataCity="contentDataCity"
                :IStitle="false" :JFData.sync="JFData" :title="'申请户主信息'" @getValues="getValues" />
            <div style="background-color: #f6f6f6;height: 10px;margin-top: 4px;"></div>
            <tableAdd v-if="JFData[0].IF_LJ == 5" :title="'联建人信息'" :contentDataCity="contentDataCity1"
                @saveLjrData="saveLjrData" :IStitle="false" :button1="button1" :contentData1="contentData1"
                :contentData="contentDataData" :JFData="JFDataLJR" />
            <div v-if="JFData[0].IF_LJ == 5" style="background-color: #f6f6f6;height: 10px;margin-top: 4px;"></div>
            <van-collapse v-model="activeNames1">
                <van-collapse-item :title="'附件'" name="1">
                    <van-button style="width: 30vw;position: absolute; right: 0; top: 62px;" size="small" type="info"
                        @click="handleCreateHtmlPdf">签署承诺书</van-button>
                    <upLoad :rowData="rowData" :FJData="FJData1" :isReadOnly="true"
                        :SHITUID="'98bcf528966e48ce896a8b3fd02d45d5'" :UpLoadID="'7a3fc061c866434b8475d3775c9ddcea'" />
                </van-collapse-item>
            </van-collapse>
        </div>
        <div class="footer" v-show="!isshowChengnuoshu">
            <van-button style="width: 30vw" size="small" type="info">保存</van-button>
            <van-button style="width: 30vw" size="small" type="info" @click="getLSH" v-preventReClick="7000">提交</van-button>
        </div>
        <div v-if="isshowChengnuoshu">
            <chengNuoShu :ChengNuoShuData="ChengNuoShuData" />
        </div>
    </div>
</template>

<script>
import bigData from '../NewZzd/public/bigData'
import inputContatiner from '../NewZzd/public/inputContatiner'
import inputSelect from '../NewZzd/public/inputSelect'
import upLoad from '@/utils/upLoad'
import Vue from 'vue'
import tableAdd from '../NewZzd/public/zlbTbaleAdd'
import chengNuoShu from './chengNuoShu'
import { preventReClick } from '@/utils/plugins'
import { GJDBFunction1, GJDBFunction3 } from '../../utils/queryData'
import { GSA_Add, GSA_Search } from "@/common/commonApi";
import { idCard } from '@/utils/idCard'
import timeFormat from "@/utils/timeFormat.js";
import { v4 as uuidv4 } from 'uuid'
import {
    QuerySignCallback,
    InsertDataView,
    PrepareCreateProcess,
    FileServerProxy,
    CreateProcess,
    PlatSendTask,
    getRegister1,
    getSystemCityCode,
    dataTransFile,
    JFDataList,
    debounce,
    dataTransFilePreview,
    getDatalistForTree,
    getDataEnumByGroupName,
    numGenerateGetNum,
    existHouse,
    JFsaveOrUpdate,
    houseApplyDelete
} from '@/utils/api'
Vue.use(preventReClick)
export default {
    data () {
        let self = this
        return {
            button1: ['添加联建人', '删除联建人'],
            activeNames1: ['1'],
            content: [],
            Isshowbutton1: true,
            isShowCity: true,
            isshowChengnuoshu: false,
            contentData: [
                {
                    label: '姓名',
                    key: 'XM', //绑定的字段
                    ISinput: true, //true:输入框
                    required: true, //true: 必填项
                    readonly: true, //true:可以填写 false：不可以填写
                    isTime: false //true:可以时间选择器 false：下拉框
                },
            ],
            contentDataData: [
                {
                    label: '姓名',
                    key: 'XM', //绑定的字段
                    ISinput: true, //true:输入框
                    required: true, //true: 必填项
                    readonly: true, //true:可以填写 false：不可以填写
                    isTime: false //true:可以时间选择器 false：下拉框
                },
                {
                    label: '身份证号',
                    key: 'ID_CARD', //绑定的字段
                    ISinput: true, //true:输入框
                    required: true, //true: 必填项
                    readonly: true, //true:可以填写 false：不可以填写
                    isTime: false //true:可以时间选择器 false：下拉框
                },
                {
                    label: '联系电话',
                    key: 'PHONE', //绑定的字段
                    ISinput: true, //true:输入框
                    required: true, //true: 必填项
                    readonly: true, //true:可以填写 false：不可以填写
                    isTime: false //true:可以时间选择器 false：下拉框
                },
                {
                    label: '性别',
                    key: 'SEX',
                    ISinput: false,
                    required: true,
                    readonly: true,
                    isTime: false,
                    emitData: 'a8e5782d-9a59-48e4-b35b-654b9bb667ca',
                    NYDARR: []
                },
                {
                    label: '年龄',
                    key: 'AGE', //绑定的字段
                    ISinput: true, //true:输入框
                    required: true, //true: 必填项
                    readonly: true, //true:可以填写 false：不可以填写
                    isTime: false //true:可以时间选择器 false：下拉框
                },
                {
                    label: '是否联建',
                    key: 'IF_LJ',
                    ISinput: false,
                    required: true,
                    readonly: true,
                    isTime: false,
                    emitData: '86d6eb80-8bcb-49f6-b5c3-462ac1ae585d',
                    NYDARR: []
                },
                // {
                //     label: '是否联建',
                //     key: 'IF_LJ', //绑定的字段
                //     ISinput: true, //true:输入框
                //     required: true, //true: 必填项
                //     readonly: false, //true:可以填写 false：不可以填写
                //     isTime: false //true:可以时间选择器 false：下拉框
                // },
                {
                    label: '户口所在地',
                    key: 'DOMICILE', //绑定的字段
                    ISinput: true, //true:输入框
                    required: true, //true: 必填项
                    readonly: true, //true:可以填写 false：不可以填写
                    isTime: false //true:可以时间选择器 false：下拉框
                },
                {
                    label: '家庭住址',
                    key: 'HOMEADDRESS', //绑定的字段
                    ISinput: true, //true:输入框
                    required: true, //true: 必填项
                    readonly: true, //true:可以填写 false：不可以填写
                    isTime: false //true:可以时间选择器 false：下拉框
                },
                {
                    label: '申请理由',
                    key: 'APPLICATION',
                    ISinput: false,
                    required: true,
                    readonly: true,
                    isTime: false,
                    emitData: '979dbb7c-d1f6-4074-a177-9ce25b0ad99e',
                    NYDARR: []
                },
                {
                    label: '是否属于变更',
                    key: 'IF_ALTER',
                    ISinput: false,
                    required: true,
                    readonly: true,
                    isTime: false,
                    emitData: '86d6eb80-8bcb-49f6-b5c3-462ac1ae585d',
                    NYDARR: []
                }
            ],
            contentData1: [
                {
                    label: '姓名',
                    key: 'XM' //绑定的字段
                },
                {
                    label: '性别',
                    key: 'SEX'
                },
                {
                    label: '身份证号',
                    key: 'ID_CARD'
                },
                {
                    label: '年龄',
                    key: 'AGE'
                }
            ],
            FJData1: [
                {
                    title: '农村宅基地使用承诺书',
                    FileName: [],
                    groupGuid: '0ee9b59f-4a90-4737-bc72-bded711ae918',
                    fileList: [],
                    isReadOnly: true,
                }
            ],
            contentDataCity: [
                {
                    label: '所属市',
                    key: 'CITY', //绑定的字段
                    ISinput: true, //true:输入框
                    required: true, //true: 必填项
                    readonly: true, //true:可以填写 false：不可以填写
                    NYDARR: []
                },
                {
                    label: '所属县(区)',
                    key: 'COUNTY', //绑定的字段
                    ISinput: true, //true:输入框
                    required: true, //true: 必填项
                    readonly: true, //true:可以填写 false：不可以填写
                    NYDARR: []
                },
                {
                    label: '所属镇',
                    key: 'TOWN', //绑定的字段
                    ISinput: true, //true:输入框
                    required: true, //true: 必填项
                    readonly: true, //true:可以填写 false：不可以填写
                    NYDARR: []
                },
                {
                    label: '所属村',
                    key: 'VILLAGE', //绑定的字段
                    ISinput: true, //true:输入框
                    required: true, //true: 必填项
                    readonly: true, //true:可以填写 false：不可以填写
                    NYDARR: []
                }
            ],
            contentDataCity1: [
                {
                    label: '所属市',
                    key: 'CITY', //绑定的字段
                    ISinput: true, //true:输入框
                    required: true, //true: 必填项
                    readonly: true, //true:可以填写 false：不可以填写
                    NYDARR: []
                },
                {
                    label: '所属县(区)',
                    key: 'COUNTY', //绑定的字段1
                    ISinput: true, //true:输入框
                    required: true, //true: 必填项
                    readonly: true, //true:可以填写 false：不可以填写
                    NYDARR: []
                },
                {
                    label: '所属镇',
                    key: 'TOWN', //绑定的字段
                    ISinput: true, //true:输入框
                    required: true, //true: 必填项
                    readonly: true, //true:可以填写 false：不可以填写
                    NYDARR: []
                },
                {
                    label: '所属村',
                    key: 'VILLAGE', //绑定的字段
                    ISinput: true, //true:输入框
                    required: true, //true: 必填项
                    readonly: true, //true:可以填写 false：不可以填写
                    NYDARR: []
                }
            ],
            contentData: [
                {
                    label: '姓名',
                    key: 'XM', //绑定的字段
                    ISinput: true, //true:输入框
                    required: true, //true: 必填项
                    readonly: false, //true:可以填写 false：不可以填写
                    isTime: false //true:可以时间选择器 false：下拉框
                },
                {
                    label: '身份证号',
                    key: 'ID_CARD', //绑定的字段
                    ISinput: true, //true:输入框
                    required: true, //true: 必填项
                    readonly: false, //true:可以填写 false：不可以填写
                    isTime: false //true:可以时间选择器 false：下拉框
                },
                {
                    label: '联系电话',
                    key: 'PHONE', //绑定的字段
                    ISinput: true, //true:输入框
                    required: true, //true: 必填项
                    readonly: false, //true:可以填写 false：不可以填写
                    isTime: false //true:可以时间选择器 false：下拉框
                },
                {
                    label: '性别',
                    key: 'SEX',
                    ISinput: false,
                    required: true,
                    readonly: false,
                    isTime: false,
                    emitData: 'a8e5782d-9a59-48e4-b35b-654b9bb667ca',
                    NYDARR: []
                },
                {
                    label: '年龄',
                    key: 'AGE', //绑定的字段
                    ISinput: true, //true:输入框
                    required: true, //true: 必填项
                    readonly: false, //true:可以填写 false：不可以填写
                    isTime: false //true:可以时间选择器 false：下拉框
                },
                {
                    label: '是否联建',
                    key: 'IF_LJ',
                    ISinput: false,
                    required: true,
                    readonly: true,
                    isTime: false,
                    emitData: '86d6eb80-8bcb-49f6-b5c3-462ac1ae585d',
                    NYDARR: []
                },
                // {
                //     label: '是否联建',
                //     key: 'IF_LJ', //绑定的字段
                //     ISinput: true, //true:输入框
                //     required: true, //true: 必填项
                //     readonly: false, //true:可以填写 false：不可以填写
                //     isTime: false //true:可以时间选择器 false：下拉框
                // },
                {
                    label: '户口所在地',
                    key: 'DOMICILE', //绑定的字段
                    ISinput: true, //true:输入框
                    required: true, //true: 必填项
                    readonly: true, //true:可以填写 false：不可以填写
                    isTime: false //true:可以时间选择器 false：下拉框
                },
                {
                    label: '家庭住址',
                    key: 'HOMEADDRESS', //绑定的字段
                    ISinput: true, //true:输入框
                    required: true, //true: 必填项
                    readonly: true, //true:可以填写 false：不可以填写
                    isTime: false //true:可以时间选择器 false：下拉框
                },
                {
                    label: '申请理由',
                    key: 'APPLICATION',
                    ISinput: false,
                    required: true,
                    readonly: true,
                    isTime: false,
                    emitData: '979dbb7c-d1f6-4074-a177-9ce25b0ad99e',
                    NYDARR: []
                },
                {
                    label: '是否属于变更',
                    key: 'IF_ALTER',
                    ISinput: false,
                    required: true,
                    readonly: true,
                    isTime: false,
                    emitData: '86d6eb80-8bcb-49f6-b5c3-462ac1ae585d',
                    NYDARR: []
                }
            ],
            rowData: {

            },
            JFData: [
                {
                    CITY: '',
                    COUNTY: '',
                    TOWN: '',
                    VILLAGE: '',
                    IF_LJ: '',
                    XM: window.localStorage.getItem('DanDianName')
                        ? self.plusXing(window.localStorage.getItem('DanDianName'), 3, 5)
                        : '',
                    SEX: '',
                    AGE: '',
                    ID_CARD: window.localStorage.getItem('DanDianid')
                        ? self.plusXing(window.localStorage.getItem('DanDianid'), 3, 5)
                        : '',
                    PHONE: window.localStorage.getItem('DanDianPhone')
                        ? self.plusXing(window.localStorage.getItem('DanDianPhone'), 3, 5)
                        : '',
                    HOMEADDRESS: '',
                    APPLICATION: '',
                    IF_ALTER: '',
                    TPYE: 1,
                    DOMICILE: '',
                    ISDELETE: 0,
                    lsh: '',
                    CREATIONTIME: ''
                }
            ],
            JFDataLJR: [
                {
                    CITY: '',
                    COUNTY: '',
                    TOWN: '',
                    VILLAGE: '',
                    IF_LJ: '',
                    XM: '',
                    SEX: '',
                    AGE: '',
                    ID_CARD: '',
                    PHONE: '',
                    DOMICILE: '',
                    HOMEADDRESS: '',
                    APPLICATION: '',
                    IF_ALTER: '',
                    TPYE: 1,
                    ISDELETE: 0,
                    lsh: '',
                    CREATIONTIME: ''
                }
            ],
            uuid: '',
            ChengNuoShuData: {

            },
        }
    },
    methods: {
        //下拉框有特定需求时用这个
        getValues (name, value, label) {
            // console.log(name, value, label)
            this.ChengNuoShuData[label] = name
        },
        handleCreateHtmlPdf () {
            if (this.JFData[0].APPLICATION && this.JFData[0].CITY && this.JFData[0].COUNTY && this.JFData[0].TOWN && this.JFData[0].VILLAGE) {
                this.isshowChengnuoshu = true
                window.localStorage.setItem('citycode',this.JFData[0].COUNTY)
            } else {
                this.$toast.fail('请先填写申请理由并且选择所在城市')
            }
        },
        async saveLjrData (data) {
            //调用接口保存数据
            console.log(data)

        },
        /**
         * 
         * @param {*} str 
         * @param {*} str 
         * @param {*} frontLen 
         * @param {*} endLen 
         */
        plusXing (str, frontLen, endLen) {
            //浙里办新规则
            if (str.length < 9) {
                return '*' + str.substring(1, str.length) //截取name 字符串截取第一个字符
            } else if (str.length > 9 && str.length < 18) {
                return str.substring(0, 3) + '****' + str.substring(7, str.length) //截取第一个和大于第4个字符
            } else {
                return str.substring(0, 1) + '****************' + str.substring(str.length - 1, str.length)
            }
        },
        /**
         * 获取流水号
         */
        async getLSH () {
            //获取流水号之前先判断当前身份证号正不正确属不属于惠州市的 不是惠州市的要提示
            //先判断是否建过房子
            var res2 = await existHouse(window.localStorage.getItem('DanDianid'))
            try {
                let data = res2.data.data
                if (!data) {
                    this.$toast.fail('系统检测到您已发起过建房申请，请勿重复提交。')
                } else {
                    let date = new Date()
                    this.JFData[0].ROW_GUID = this.uuid
                    this.JFData[0].TO_ROW_GUID = this.uuid
                    this.JFData[0].CREATIONTIME = timeFormat(null, "yyyy-mm-dd hh:MM:ss");
                    let year = date.getFullYear()
                    let params = {
                        appKey: '6738b790-69e8-11ec-a673-6805cab640z1',
                        accessCode: 'b03be941dc27498ebcc46311330ee97d',
                        keyType: '3',
                        numPrefix: 'SPJF-' + this.JFData[0].VILLAGE, //SPJF-村级citycode
                        year: year, //当前年份
                    }
                    const res = await numGenerateGetNum(params)
                    if (res.data.code == 200) {
                        this.JFData[0].lsh = res.data.data
                        //有了流水号在往下发
                        this.saveData()
                    } else {
                        //获取流水号失败不允许往下发
                        this.$toast.fail('获取流水号失败')
                    }
                }
            } catch (err) {
                //添加节流函数
                this.$toast.fail('系统检测到您已发起过建房申请，请勿重复提交。')
            }

        },
        /**
         * 保存数据
         */

        async saveData () {
            //判断必填项
            let num = 0
            console.log(this.JFData)
            this.JFData.forEach(item => {
                for (let i in item) {
                    if (item[i] === '' || item[i] == null) {
                        console.log(item[i])
                        num++
                    }
                }
            })
            //如果是否联建是联建，则联建单位必填
            if (this.JFData[0].IF_LJ == '1') {

                if (num) {
                    this.$toast.fail('请填写联建单位')
                    return
                }
            }
            if (num) {
                this.$toast.fail('请填写必填项')
                return
            }
            this.JFData[0].PHONE = window.localStorage.getItem('DanDianPhone')
            this.JFData[0].ID_CARD = window.localStorage.getItem('DanDianid')
            this.JFData[0].XM = window.localStorage.getItem('DanDianName')
            window.localStorage.setItem('citycode',this.JFData[0].COUNTY)
            //成功之后需要将数据推送到gsa平台
            let paramss = {
                "address": this.JFData[0].HOMEADDRESS,
                "applyphone": this.JFData[0].PHONE,
                "applytime": this.JFData[0].CREATIONTIME,  // yyyy-MM-dd HH:mm:ss
                "cun": this.JFData[0].VILLAGE,
                rowGuid: this.JFData[0].ROW_GUID,
                "idcard": this.JFData[0].ID_CARD,
                "personname": this.JFData[0].XM,
                "s": this.JFData[0].CITY,
                "serialNumber": this.JFData[0].lsh,
                "town": this.JFData[0].TOWN,
                "x": this.JFData[0].COUNTY,
                'versionNumber':"1"
            }
            const res11 = await JFsaveOrUpdate(paramss)
            this.JFData[0].VERSIONNUMBER = 1
            if (res11.data.data) {
                const res = await GSA_Add('931c475d-c6f3-41c0-8aef-28e20922db97', true, ...this.JFData)
                if (!res) {
                    this.$toast.fail("提交失败请稍后重试!");
                    let per = {
                        rowGuid: this.$store.state.toGuid.row_guid,
                        "serialNumber": data1.lsh,
                    }
                    const result = await houseApplyDelete(per)
                } else {
                    setTimeout(() => {
                        this.$router.push({
                        path: '/zljindex',
                    })
                    }, 500)
                }
            } else {
                this.$toast.fail("提交失败请稍后重试!");
            }

        }
    },
    mounted () {
        window.localStorage.setItem('isGJM', true)
        this.JFData[0].AGE = idCard(window.localStorage.getItem('DanDianid'), 3)
        this.JFData[0].SEX = idCard(window.localStorage.getItem('DanDianid'), 2) == '男' ? '3' : '4'
        //生成主申请人的row_guid == 自己的to_row_guid = 多个联建人的to_row_guid
        this.uuid = uuidv4()
        this.rowData.ROW_GUID = this.uuid
        window.sessionStorage.setItem('uuidv4', this.uuid)
    },
    created () { },
    components: {
        bigData,
        inputContatiner,
        inputSelect,
        upLoad,
        tableAdd,
        chengNuoShu
    }
}
</script>

<style lang="scss" scoped>
.ceshi {
    height: 100vh;

    .CUNYUSHEN {
        height: 93vh;
        padding: 4px;
        overflow: auto;
    }

    .footer {
        height: 6vh;
        display: flex;
        justify-content: space-around;
        align-items: center;
    }

    .subTitle {
        width: 100%;
        height: 40px;
        line-height: 40px;
        font-size: 14px;
        position: relative;
        display: flex;
        align-items: center;
        border-bottom: 1px solid #9b9b9b;
        padding-left: 10px;

        .line {
            width: 6px;
            height: 26px;
            background: #3f8ffc;
            display: inline-block;
            margin-right: 10px;
        }
    }
}
</style>

<style>
.van-cell {
    color: black;
    font-weight: bold !important;
}

.van-collapse-item__content {
    color: black;
    padding: 0 6px !important;
}

.ceshi .van-cell__title {
    font-size: 18px;
}
</style>
